// pages/user/Resume/Resume.js
import store from '../../../store/index'
import create from '../../../utils/create'
import api from '../../../api/index'

create(store, {

  /**
   * 页面的初始数据
   */
  data: {
    use: ['user'],
    orderId: '',
    content: '',
    imgs: [],
    attach: '',
    attachName: '点击上传附件',
    attachPath: '',
    active: false,
    resume: []


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      resume: store.data.user.resume
    })
  },
  tapAttach: function () {
    // 我的简历数小于3时，才可以上传
    if (store.fileList.resumeNum < 3) {
      var self = this
      api.file.uploadResume().then(res => {
        console.log(res);
        const that = self;
        store.refreshUser().then(res => {
          console.log(res);
          store.fileList.resumeNum = res.resume.length;
          that.setData({
            resume: res.resume
          })
        })
      })
    } else {
      wx.showToast({
        title: '文件数已达上限',
        icon:'error'
      })
    }

  },

  // 刪除所有文件
  deleteAll: function () {
    var file = store.data.user.resume.file
    var params = { file }
    var self = this
    wx.showActionSheet({
      itemList: ['确定要删除所有简历吗'],//显示的列表项
      success: function (res) {//res.tapIndex点击的列表项
        console.log("执行删除操作")
        const that = self
        api.file.del(params).then(res => {
          console.log(res);
          store.fileList.resumeNum = 0;
          store.refreshUser().then(res => {
            that.setData({
              resume: res.resume,
              works: res.works
            })
          })
        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })

  },

  open: async function (e) {
    var index = e.currentTarget.dataset.index
    var file = store.data.user.resume[index].file
    var fileList = []
    fileList.push(file)
    const params = { fileList }
    wx.showLoading({ title: '打开中' })
    api.file.downloadResume(params).then(res => {
      console.log(res.massage)
      wx.hideLoading()
    })
  },
  more: function (e) {

    console.log(e)
    var index = e.currentTarget.dataset.index
    var file = store.data.user.resume[index].file
    var params = { file }
    var self = this
    wx.showActionSheet({
      itemList: ['删除'],//显示的列表项
      success: function (res) {//res.tapIndex点击的列表项
        console.log("执行删除操作")
        const that = self
        api.file.del(params).then(res => {
          console.log(res)
          store.refreshUser().then(res => {
            that.setData({
              resume: res.resume,
              works: res.works
            })
          })
        })
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  },

})